<?php
/**
 * uft-8字符串截取插件
 * @author chenhao
 * @copyright 成都四方客信息技术有限公司
 * @link http://www.sifangke.cn/
 * @since Wed Nov 18 17:57:20 CST 2009
 * @example 
 * **************************
 * {$content|utf8cut:20}
 * **************************
 */
 

function smarty_function_utf8cut($string, $leng=100, $dot='...') {
if(!isset($string) || $string == '') {
  return '';
}
preg_match_all("/.{1}/", $string, $chars);
$c = '';
$all = array();
$timer = 0;
foreach($chars[0] as $char) {
  $timer++;
  if(ord($char) > 127) {
   $c .= $char;
   if($timer == 3) {
    $all[] = $c;
    $c = '';
    $timer = 0;
   }
  } else {
   $c = $char;
   $timer = 0;
   $all[] = $c;
   $c = '';
  }
}
if(sizeof($all) <= $leng) {
  return implode('', $all);
}
return implode('',array_slice($all, 0, $leng)).$dot;
}
?>